package com.fz.service.impl;

import cn.hutool.core.lang.Dict;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fz.common.GlobalAll;
import com.fz.common.ResponseEntity;
import com.fz.dto.system.dictionary.DictQuery;
import com.fz.entity.system.Area;
import com.fz.entity.system.Dictionary;
import com.fz.entity.system.DictionaryValue;
import com.fz.mapper.system.AreaMapper;
import com.fz.mapper.system.DictionaryMapper;
import com.fz.mapper.system.DictionaryValueMapper;
import com.fz.service.CommonService;
import com.fz.vo.system.area.AreaTreeVo;
import com.fz.vo.system.area.AreaVo;
import com.fz.vo.system.dictionary.DictionaryTreeVo;
import com.fz.vo.system.dictionaryValue.DictionaryValueVo;
import org.springframework.beans.BeanUtils;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Service
@SuppressWarnings("unchecked")
public class CommonServiceImpl implements CommonService {
    @Resource
    private DictionaryMapper dictionaryMapper;
    @Resource
    private DictionaryValueMapper dictionaryValueMapper;

    @Resource
    private AreaMapper areaMapper;

    /**
     * @方法描述: 根据字典key获取字典名
     * @参数:
     * @返回值:
     * @创建人: I am God
     * @创建时间:: 2021/3/9
     */
    @Override
    public ResponseEntity<Dictionary> findDict(String dictKey) throws Exception {
        QueryWrapper<Dictionary> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dictionary_key", dictKey);
        queryWrapper.eq("delete_status", GlobalAll.DELETE_STATUS_NO);
        List<Dictionary> list = dictionaryMapper.selectList(queryWrapper);
        if (list != null && list.size() > 0) {
            return ResponseEntity.buildSuccess(list.get(0));
        } else {
            return ResponseEntity.buildSuccess(null);
        }
    }


    /**
     * @方法描述: 根据字典表ID 获取所对应的key 与 value
     * @参数:
     * @返回值:
     * @创建人: I am God
     * @创建时间:: 2021/3/9
     */
    @Override
    public ResponseEntity<List<DictionaryValue>> findDictValue(String dictid) throws Exception {
        QueryWrapper<DictionaryValue> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dictionary_id", dictid);
        queryWrapper.eq("delete_status", GlobalAll.DELETE_STATUS_NO);
        queryWrapper.orderByAsc("sort");
        List<DictionaryValue> list = dictionaryValueMapper.selectList(queryWrapper);
        if (list != null && list.size() > 0) {
            return ResponseEntity.buildSuccess(list);
        } else {
            return ResponseEntity.buildSuccess(null);
        }
    }


    @Override
    @Cacheable(value = "#p0")
    public ResponseEntity<AreaVo> findAreaList(String code) {
        LambdaQueryWrapper<Area> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Area::getParentCode, code);
        List<AreaVo> areas = areaMapper.selectAreaList(wrapper);
        if (!areas.isEmpty()) {
            return ResponseEntity.buildSuccess(areas);
        } else {
            return ResponseEntity.buildSuccess(null);
        }
    }
}
